LightOJ - 1258 Making Huge Palindromes (马拉车的应用)
题目描述
A string is said to be a palindrome if it remains same when read backwards. So, ‘abba’, ‘madam’ both are palindromes, but ‘adam’ is not.
Now you are given a non-empty string S, containing only lowercase English letters. The given string may or may not be palindrome. Your task is to make it a palindrome. But you are only allowed to add characters at the right side of the string. And of course you can add any character you want, but the resulting string has to be a palindrome, and the length of the palindrome should be as small as possible.
For example, the string is ‘bababa’. You can make many palindromes including
bababababab
babababab
bababab
Since we want a palindrome with minimum length, the solution is ‘bababab’ cause its length is minimum.
Input
Input starts with an integer T (≤ 10), denoting the number of test cases.
Each case starts with a line containing a string S. You can assume that 1 ≤ length(S) ≤ 106.
Output
For each case, print the case number and the length of the shortest palindrome you can make with S.
Sample Input
1 | 4 |
Sample Output
1 | Case 1: 11 |
Note
Dataset is huge, use faster I/O methods.
题解
题目大意是给你n个字符串, 让你至少补多少个字母, 能使其成为回文串, 并输出补充之后的字符串的长度最短
要是不明白马拉车的实现原理, 那就给你推荐两篇博客:
博客1
博客2
就是用马拉车求包含最后一个字符的最长回文串, 那就在马拉车那里动手脚咯, 详情请看代码注释
AC代码
1 |
|
1 | 恰似你一低头的温柔,娇弱水莲花不胜寒风的娇羞, 我的心为你悸动不休。 --mingfuyan |
原文作者: Mingfu Yan
原文链接: https://solodance.top/2020/06/26/LightOJ - 1258 Making Huge Palindromes (马拉车的应用)/
版权声明: 转载请注明出处(必须保留作者署名及链接)